package com.yy.jhl.ui.user.activity;

import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.FormatUtil;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.interfaces.CommonCallBack;
import com.yy.jhl.ui.user.contract.RegisterContract;
import com.yy.jhl.ui.user.presenter.RegisterPresenter;
import com.yy.jhl.util.DataRequestUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Ellis.Li on 2017/8/30.
 */

public class RegisterActivity extends BaseActivity<RegisterPresenter> implements RegisterContract.View {
    @BindView(R.id.phone_edit)
    EditText phoneEdit;
    @BindView(R.id.code_edit)
    EditText codeEdit;
    @BindView(R.id.send_code_btn)
    Button sendCodeBtn;
    @BindView(R.id.pwd_edit)
    EditText pwdEdit;
    @BindView(R.id.ensure_btn)
    Button ensureBtn;
    @BindView(R.id.login_parent)
    LinearLayout loginParent;

    @Override
    public int getLayoutId() {
        return R.layout.act_register;
    }

    @Override
    public void initView() {
        showTitle("注册", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        sendCodeBtn.setClickable(false);
        phoneEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String phone = charSequence.toString();
                if (!StringUtil.isEmpty(phone)){
                    sendCodeBtn.setClickable(true);
                    sendCodeBtn.setBackgroundResource(R.drawable.green_btn_bg);
                }else {
                    sendCodeBtn.setClickable(false);
                    sendCodeBtn.setBackgroundResource(R.drawable.gray_btn_bg);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void registerSuccess() {
        showShortToast("注册成功！");
        startActivity(LoginActivity.class);
        finish();
    }

    @Override
    public void registerFail() {

    }
    @OnClick({R.id.send_code_btn, R.id.ensure_btn, R.id.login_parent})
    public void onViewClicked(View view) {
        String phone = phoneEdit.getText().toString();
        String code = codeEdit.getText().toString();
        String pwd = pwdEdit.getText().toString();
        switch (view.getId()) {
            case R.id.send_code_btn:
                if (StringUtil.isEmpty(phone)){
                    showShortToast("手机号不能为空");
                    return;
                }
                if (!FormatUtil.isMobileNO(phone)){
                    showShortToast("手机号格式不正确");
                    return;
                }
                DataRequestUtil.getInstance(mContext).sendVerificationCode(phone, new CommonCallBack() {
                    @Override
                    public void getCallBack() {
                        showShortToast("验证码发送成功");
                        sendCodeBtn.setClickable(false);
                        sendCodeBtn.setBackgroundResource(R.drawable.gray_btn_bg);
                        min = 60;
                        timeRunnable.run();
                    }
                });
                break;
            case R.id.ensure_btn:
                if (StringUtil.isEmpty(phone)){
                    showShortToast("手机号不能为空");
                    return;
                }
                if (!FormatUtil.isMobileNO(phone)){
                    showShortToast("手机号格式不正确");
                    return;
                }
                if (StringUtil.isEmpty(code)){
                    showShortToast("验证码不能为空");
                    return;
                }
                if (StringUtil.isEmpty(pwd)){
                    showShortToast("密码不能为空");
                    return;
                }
                if (pwd.length() < 6 || pwd.length() > 30){
                    showShortToast("请输入6-30位的新密码");
                    return;
                }
                mPresenter.register(phone,"",code,pwd,pwd,1);
                break;
            case R.id.login_parent:
                startActivity(LoginActivity.class);
                finish();
                break;
        }
    }
    Handler handler = new Handler(){};
    int min = 60;
    /** 倒计时*/
    Runnable timeRunnable = new Runnable() {
        @Override
        public void run() {
            try {
                if(min > 0){
                    min--;
                    sendCodeBtn.setClickable(false);
                    sendCodeBtn.setBackgroundResource(R.drawable.gray_btn_bg);
                    sendCodeBtn.setText(min + "s");
                    handler.postDelayed(timeRunnable, 1000);
                }else{
                    sendCodeBtn.setText("获取验证码");
                    sendCodeBtn.setClickable(true);
                    sendCodeBtn.setBackgroundResource(R.drawable.green_btn_bg);
                }
            }catch (Exception e){
            }
        }
    };
}
